package com.annet.token.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;


/**
 * @author panlin
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor tokenInterceptor;

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }

    /**
     * 拦截器配置
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 需要排除的地址
        String[] excludePath = {
                // 带后缀的静态资源
                "/**/**.*",
                "/**.*",
                // 错误处理页面
                "/error",
                "/error/**"
//
//                "/config/**",
//                "/user/**",
//                // 医生注册登录
//                "/doctor/login",
//                "/doctor/updatePassword",
//                "/doctor/loginOrBindPhone",
//                "/doctor/login/allHospital",
//                // 患者注册登录
//                "/patient/openId",
//                "/patient/register",
//                "/patient/login",
//                "/patient/loginByOpenId",
//                "/patient/updatePassword",
//                "/patient/loginOrBindPhone",
//                "/patient/loginOrBindPhoneNew",
//                "/v2/patient/registerLogin",
//                "/thirdpatry/cmcc2/patient/registerLogin",
//                "/thirdpatry/cmcc2/patient/getPhoneByToken",
//                "/thirdpatry/cmcc2/patient/registerOrLogin",
//                // 手机短信
//                "/phone/**",
//                "/hospital/get/**",
//                // 微信支付成功回调
//                "/order/callback/**",
//                // 服务号接口
//                "/serve/**",
//                // im群消息回调
//                "/im/**",
//                "/im/callback/**",
//                // 测试地址
//                "/test/**",
//                // 手机号白名单
//                "/demoPhone/**",
//                // 错误页面
//                "/error/**",
//                "/followup/**/**.*",
//                "/imageControlWX/**/**.*",
//                "/pc/**/**.*",
//                "/qrcode/**/**.*",
//                "/recipe/**/**.*",
//                "/writeRecipe/**/**.*",
//
//                "/hospital/**",
//                "/patient/recentDoctor",
//                "/doctor/recommend",
//                "/doctor/detail/**",
//                "/doctor/getDoctorInfoByUserId",
//                "/doctor/searchDoctor",
//                "/doctor/searchByName",
//                "/doctor/getEnterpriseDoctorList",
//
//                "/department/**",
//                "/consult/chat/**",
//                "/patient/isAttention",
//                "/position/all",
//                "/szlh/advice/hospitalAdvicePush",
//                "/szlh/weChatEvent/**",
//                "/szlh/medicalInsurance/**",
//                // 管理端模板消息推送
//                "/v2/temMessageSend/**",
//                "/v2/chinaArea/**",
//                "/oss/getAliKey/**",
//
//                "/v2/cmcc/auth/getToken",
//                "/v2/appletFunc/**",
//                "/thirdpatry/cmcc2/asiainfo/**",
//                "/tencent/**",
//                "/thirdparty/fdsz/clinc/**"
        };
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath)
                .order(2);
    }

    /**
     * 跨域配置
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 设置允许跨域请求的域名
        config.addAllowedOrigin("*");
        // 是否允许证书 不再默认开启
        // cfg.setAllowCredentials(true);
        // 设置允许的方法
        config.addAllowedMethod("*");
//        config.addAllowedMethod(HttpMethod.OPTIONS);
//        config.addAllowedMethod(HttpMethod.GET);
//        config.addAllowedMethod(HttpMethod.POST);
//        config.addAllowedMethod(HttpMethod.HEAD);
        // 允许任何头
        config.addAllowedHeader("*");
        // 运行跨域的header值
        config.addExposedHeader("token");
        // 跨域允许时间
        config.setMaxAge(3600L);
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        // 拦截所有路径
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }

    /**
     * 安全验证过滤器
     *
     * @return
     */
/*    @Bean
    public FilterRegistrationBean pathFilter() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new SecurityFilter());
        registration.setName("SecurityFilter");
        registration.addUrlPatterns("/*");
        registration.setOrder(1);
        registration.setEnabled(true);
        return registration;
    }*/

}
